﻿Imports System.IO
Imports BITC.DatabaseManagement
Imports Microsoft.SqlServer.Management.Smo
Imports System.Text

Public Class frmMain

#Region "Declaration"

#End Region

#Region "Constructor"

#End Region

#Region "Property"

#End Region

#Region "Method"

    ''' <summary>
    ''' Hiển thị form Connect to server
    ''' </summary>
    ''' <remarks>NDTRUNG - 12/2/2012</remarks>
    Private Sub ShowConnectForm()
        frmServer.StartPosition = FormStartPosition.CenterScreen
        AddHandler frmServer.Closed, AddressOf frmServer_Closed
        frmServer.ShowDialog()
    End Sub

    Private Sub PopulateTreeView()
        Dim rootNode As New TreeNode(SMOHelper.Current.ServerName)

        rootNode.ContextMenuStrip = ctmServer
        For Each item As Database In SMOHelper.Current.Databases()
            If Not item.IsSystemObject Then
                Dim childNode As New TreeNode(item.Name)
                childNode.ContextMenuStrip = ctmDatabase
                rootNode.Nodes.Add(childNode)
            End If
        Next

        tvDatabase.Nodes.Add(rootNode)
        tvDatabase.ExpandAll()
    End Sub

#End Region

#Region "Event"

#Region "Form"

    Private Sub frmMain_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
        Try
            ShowConnectForm()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub frmServer_Closed(ByVal sender As Object, ByVal e As EventArgs)
        Try
            Dim frm As frmServer = DirectCast(sender, frmServer)
            RemoveHandler frm.Closed, AddressOf frmServer_Closed

            If SMOHelper.Current IsNot Nothing Then
                PopulateTreeView()
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

#End Region

#Region "Menu Event"

    Private Sub mnuExit_Click(ByVal sender As Object, ByVal e As EventArgs) Handles mnuExit.Click
        Try
            Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub mnuConnect_Click(ByVal sender As Object, ByVal e As EventArgs) Handles mnuConnect.Click
        Try
            ShowConnectForm()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

#End Region

#Region "Context Menu"

    Private Sub mnuRestore_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles mnuRestore.Click
        Try
            AddHandler frmRestoreDatabase.RestoreCompleted, AddressOf frmRestoreDatabase_RestoreCompleted
            frmRestoreDatabase.ShowDialog()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub frmRestoreDatabase_RestoreCompleted(ByVal sender As Object, ByVal e As RestoreCompletedEventArgs)
        Try
            tvDatabase.SelectedNode.Nodes.Add(e.RestoredDatabase)
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub mnuBackup_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles mnuBackup.Click
        Try
            frmBackupDatabase.DatabaseName = tvDatabase.SelectedNode.Text
            frmBackupDatabase.ShowDialog()
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub mnuDelete_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles mnuDelete.Click
        Try
            If MessageBox.Show(MessageResource.ConfirmDelete, MainResource.ConfirmDelete, MessageBoxButtons.OKCancel, MessageBoxIcon.Exclamation) = DialogResult.OK Then
                SMOHelper.Current.DetachDatabase(tvDatabase.SelectedNode.Text)
                tvDatabase.Nodes.Remove(tvDatabase.SelectedNode)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

#End Region

#Region "Toolbar"

    Private Sub tbrScript_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles tbrScript.Click
        Try
            With frmQuery
                .MdiParent = Me
                .WindowState = FormWindowState.Maximized
                .Show()
            End With
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

#End Region

    Private Sub mnuOpenScript_Click(ByVal sender As System.Object, ByVal e As EventArgs) Handles mnuOpenScript.Click
        Try
            Dim result As Boolean = ofdScriptFile.ShowDialog()
            If result Then
                Dim sr As New StreamReader(ofdScriptFile.OpenFile(), Encoding.UTF8)
                With frmQuery
                    .Script = sr.ReadToEnd()
                    .MdiParent = Me
                    .WindowState = FormWindowState.Maximized
                    .Show()
                End With
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

    Private Sub tvDatabase_NodeMouseClick(ByVal sender As Object, ByVal e As TreeNodeMouseClickEventArgs) Handles tvDatabase.NodeMouseClick
        Try
            If e.Button = MouseButtons.Right Then
                tvDatabase.SelectedNode = e.Node
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

#End Region

    Private Sub tbrExecuteScript_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles tbrExecuteScript.Click
        Try
            If Not String.IsNullOrEmpty(frmQuery.txtQuery.Text) Then
                SMOHelper.Current.RunScript(frmQuery.txtQuery.Text)
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub
End Class